package fr.jade.event.filters;

import org.objectweb.fractal.api.NoSuchInterfaceException;
import org.objectweb.fractal.api.control.IllegalBindingException;
import org.objectweb.fractal.api.control.IllegalLifeCycleException;

import fr.jade.base.JadeBaseComponent;
import fr.jade.event.Event;
import fr.jade.event.EventHandler;
import fr.jade.util.JadeException;

public abstract class BaseEventFilter extends JadeBaseComponent implements EventHandler{
  
  // client interface
  protected static final String ehItfName="event-out";
  protected EventHandler eh;
  
  // event handler
  public abstract void handleEvent(Event e) throws JadeException;

  // binding controller
  @Override
  public void bindFc(String itfName, Object itfValue) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
    if ( itfName.equals(ehItfName))
      eh = (EventHandler)itfValue;
    else 
      super.bindFc(itfName, itfValue);
  }

  @Override
  public String[] listFc() {
    String[] listItf = new String[] {ehItfName};
    return super.concat(listItf, super.listFc());
  }

  @Override
  public Object lookupFc(String itfName) throws NoSuchInterfaceException {
    if ( itfName.equals(ehItfName) ){
      return eh;
    }else{
      return super.lookupFc(itfName);
    }
  }

  @Override
  public void unbindFc(String itfName) throws NoSuchInterfaceException, IllegalBindingException, IllegalLifeCycleException {
    if ( itfName.equals(ehItfName))
      eh = null;
    else
      super.unbindFc(itfName);
  }
    
}
